package org.example.threadobjectclasscommonmethods;

/**
 * @author jl
 */
public class WaitNotifyAll implements Runnable {

  //新建静态锁
  private static final Object look = new Object();

  public static void main(String[] args) {
    //新建Runnable对象
    WaitNotifyAll waitNotifyAll = new WaitNotifyAll();
    //新建线程1,2,执行方法
    Thread thread1 = new Thread(waitNotifyAll);
    Thread thread2 = new Thread(waitNotifyAll);
    thread1.start();
    thread2.start();
    try {
      Thread.sleep(300);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    new Thread(() -> {
      synchronized (look) {
        System.out.println("释放线程执行了工作");
        look.notifyAll();
        System.out.println("释放线程执行了工作结束");
      }
    }).start();

  }


  @Override
  public void run() {
    synchronized (look) {
      System.out.println(Thread.currentThread().getName() + "开始了任务");
      try {
        System.out.println(Thread.currentThread().getName() + "释放了锁");
        look.wait();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
      System.out.println(Thread.currentThread().getName() + "结束了任务");
    }
  }
}
